//
//  ACSystemCacheViewController.m
//  LFUCache_Example
//
//  Created by autohome on 2021/8/16.
//  Copyright © 2021 caixiang305621856. All rights reserved.
//

#import "ACSystemCacheViewController.h"
@interface Dog:NSObject
@property (copy, nonatomic) NSString *name;
@end

@implementation Dog
@end

@interface Person:NSObject
@property (copy, nonatomic) NSString *age;
@end

@implementation Person
@end

@interface ACSystemCacheViewController ()<NSCacheDelegate>
{
    NSMapTable *weakStrongMapTable;
    NSMapTable *strongWeakMapTable;
    NSMapTable *weakWeakMapTable;
}
@property (strong, nonatomic) NSCache *cache;

@end

@implementation ACSystemCacheViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self beginCache];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"removeAllObjects");
        [self.cache removeAllObjects];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"beginCache again");
            [self beginCache];
        });
        [[UIApplication sharedApplication] performSelector:@selector(_performMemoryWarning)];
    });
//     weakStrongMapTable = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsWeakMemory valueOptions:NSPointerFunctionsStrongMemory];
//     strongWeakMapTable = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory];
//     weakWeakMapTable = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsWeakMemory valueOptions:NSPointerFunctionsWeakMemory];
//
//    Person *per = [Person new];
//    Dog *dog = [Dog new];
//    [weakStrongMapTable setObject:dog forKey:per];
//    [strongWeakMapTable setObject:dog forKey:per];
//    [weakWeakMapTable setObject:dog forKey:per];
//    NSLog(@"里面的 weakStrongMapTable %@ \n strongWeakMapTable%@ \nweakWeakMapTable %@",weakStrongMapTable,strongWeakMapTable,weakWeakMapTable);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    NSLog(@"-----didReceiveMemoryWarning----");
}

- (void)beginCache {
    for (int i = 0; i<30; i++) {
        NSString *obj = [NSString stringWithFormat:@"object--%d",i];
        [self.cache setObject:obj forKey:@(i)];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    NSLog(@"weakStrongMapTable %@ \n strongWeakMapTable%@ \nweakWeakMapTable %@",weakStrongMapTable,strongWeakMapTable,weakWeakMapTable);
//    return;
    for (int i = 15; i < 30; i ++) {
        NSString *s =   [self.cache objectForKey:@(i)];
        NSLog(@"%@ touchesBegan ",s);
    }
}

- (NSCache *)cache {
    if (!_cache) {
        _cache = [NSCache new];
        _cache.countLimit = 0;
        _cache.delegate = self;
    }
    return _cache;
}

#pragma mark - NSCacheDelegate

- (void)cache:(NSCache *)cache willEvictObject:(id)obj {
    //evict : 驱逐
    NSLog(@"%@", [NSString stringWithFormat:@"%@ 被移除",obj]);
}

- (void)dealloc {
    NSLog(@"%s",__func__);
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
